<template>
  <div class="container">
    <div v-show="!showDetails">
      <div style="text-align: left;">
        <table>
          <tr>
            <th>
              <span>学期：</span>
              <el-button
                @click="openSemesterList = true"
                type="text"
                style="width: 150px; text-align: left;"
              >
                <span v-if="semesterSelect">{{ semesterSelect.name }}</span>
                <span v-else>未选择</span>
              </el-button>
            </th>
            <th>
              <span>班级：</span>
              <el-button
                @click="openClassList = true"
                type="text"
                style="width: 150px; text-align: left;"
              >
                <span v-if="classSelect">{{ classSelect.name }}</span>
                <span v-else>未选择</span>
              </el-button>
            </th>
            <th>
              <span>课程：</span>
              <el-button
                @click="openCourseList = true"
                type="text"
                style="width: 150px; text-align: left;"
              >
                <span v-if="courseSelect">{{ courseSelect.name }}</span>
                <span v-else>未选择</span>
              </el-button>
            </th>
            <th>
              <el-button type="primary" icon="el-icon-search">搜索</el-button>
            </th>
          </tr>
        </table>

        <!-- 学期筛选 -->
        <el-dialog title="选择学期" :visible.sync="openSemesterList">
          <el-radio-group v-model="semesterSelect">
            <el-radio
              v-for="(semester, index) in semesterList"
              :label="semester"
              :key="'semester' + index"
              >{{ semester.name }}</el-radio
            >
          </el-radio-group>
          <div slot="footer" class="dialog-footer">
            <el-button @click="semesterSelect = ''">清 空</el-button>
            <el-button type="primary" @click="openSemesterList = false"
              >确 定</el-button
            >
          </div>
        </el-dialog>
        <!-- 班级筛选 -->
        <el-dialog title="选择班级" :visible.sync="openClassList">
          <el-input placeholder="请输入关键字" v-model="classKeyword">
            <template slot="prepend"
              >班级名称</template
            >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click=""
            ></el-button>
          </el-input>
          <br />
          <br />
          <el-radio-group v-model="classSelect">
            <el-radio
              v-for="(_class, index) in classList"
              :label="_class"
              :key="'class' + index"
              >{{ _class.name }}</el-radio
            >
          </el-radio-group>
          <div slot="footer" class="dialog-footer">
            <el-button @click="classSelect = ''">清 空</el-button>
            <el-button type="primary" @click="openClassList = false"
              >确 定</el-button
            >
          </div>
        </el-dialog>
        <!-- 课程筛选 -->
        <el-dialog title="选择课程" :visible.sync="openCourseList">
          <el-input placeholder="请输入关键字" v-model="courseKeyword">
            <template slot="prepend"
              >课程名</template
            >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getCourseListByKeyword()"
            ></el-button>
          </el-input>
          <br />
          <br />
          <el-radio-group v-model="courseSelect">
            <el-radio
              v-for="(course, index) in courseList"
              :key="'course' + index"
              :label="course"
              >{{ course.name }}</el-radio
            >
          </el-radio-group>
          <div slot="footer" class="dialog-footer">
            <el-button @click="courseSelect = ''">清 空</el-button>
            <el-button type="primary" @click="openCourseList = false"
              >确 定</el-button
            >
          </div>
        </el-dialog>
      </div>
      <hr />
      <el-table
        :data="
          classCourseList.slice(
            (currentPage - 1) * pagesize,
            currentPage * pagesize
          )
        "
        stripe
        border
        @row-click="rowClick"
      >
        <el-table-column prop="semesterName" label="学期"></el-table-column>
        <el-table-column
          prop="instructionClassName"
          label="班级"
        ></el-table-column>
        <el-table-column prop="courseName" label="课程"></el-table-column>
      </el-table>

      <br />
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableTotal"
        style="margin-left: 30%;"
      ></el-pagination>
    </div>
    <!-- 详情 -->
    <div v-show="showDetails">
      <classCourseDetails
        :classCourseDetails="classCourseDetails"
        @closeDetails="closeDetails"
      />
    </div>
  </div>
</template>

<script>
import classCourseDetails from "@/views/education-instructionalMatters/class-course/details";
import { getClassCourseList } from "@/api/class-course";
import { getAllSemester } from "@/api/semester";
import { getAllCourse } from "@/api/course";
import { getAllInstructionClass } from "@/api/instructionClass";

export default {
  name: "teachingIntention",
  data() {
    return {
      //分页
      tableTotal: 6,
      currentPage: 1,
      pagesize: 10,
      showDetails: false, //显示详情页面
      classCourseDetails: {}, //某一行的详情信息
      classCourseList: [], //班级课程列表

      // 条件搜索
      // 课程
      openCourseList: false,
      courseKeyword: "",
      courseList: [],
      courseSelect: "",

      //学期
      openSemesterList: false,
      semesterKeyword: "",
      semesterList: [],
      semesterSelect: "",

      //班级
      openClassList: false,
      classKeyword: "",
      classList: [],
      classSelect: ""
    };
  },
  components: {
    classCourseDetails
  },
  methods: {
    // 获取所有班级课程列表
    getClassCourseList() {
      getClassCourseList().then(res => {
        this.tableTotal = res.data.classCourseList.length;
        this.classCourseList = res.data.classCourseList;
      });
    },

    // 获取学期列表
    getAllSemester() {
      getAllSemester().then(res => {
        this.semesterList = res.data.semesterList;
      });
    },

    //获取课程列表
    getAllCourse() {
      getAllCourse().then(res => {
        this.courseList = res.data.courseList;
      });
    },

    //获取班级列表
    getAllInstructionClass() {
      getAllInstructionClass().then(res => {
        this.classList = res.data.instructionClassList;
      });
    },

    //点击某一列,显示详情
    rowClick(val) {
      this.classCourseDetails = val;
      this.showDetails = true;
    },

    //详情组件传过来的数据
    closeDetails(val) {
      this.showDetails = val;
    },

    // 换页
    handleSizeChange: function(size) {
      this.pagesize = size;
    },
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage;
    }
  },
  created() {
    this.getClassCourseList();
    this.getAllSemester();
    this.getAllCourse();
    this.getAllInstructionClass();
  }
};
</script>

<style scoped>
.container {
  width: 80%;
  max-width: 100%;
  margin: 20px auto;
}
span {
  font-weight: 500;
}
th {
  text-align: left;
  width: 500px;
}
</style>
